package com.qiangesoft.wechat.core.handler.message;

import com.qiangesoft.wechat.core.builder.TextOutMessageBuilder;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 位置处理器
 *
 * @author qiangesoft
 * @date 2023-09-07
 */
@Slf4j
@Component
public class LocationHandler implements WxMpMessageHandler {

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) throws WxErrorException {
        if (!wxMessage.getMsgType().equals(WxConsts.XmlMsgType.LOCATION)) {
            return null;
        }

        log.info("Received wechat location message, longitude [{}], latitude [{}], precision [{}] !", wxMessage.getLongitude(), wxMessage.getLatitude(), wxMessage.getPrecision());

        String content = "当前位置：" + wxMessage.getLabel();
        return new TextOutMessageBuilder().build(content, wxMessage, wxMpService);
    }

}
